﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace EzDesk.DevHelper.FeedTemplateTool
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        XElement thisTemplate;
        XElement currentXml;
        string currentFileName;
        Dictionary<string, string> parameters;
        bool isEditModel = false;

        Preview pForm;

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            var dl = new OpenFileDialog();
            if (dl.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                XElement root = XElement.Load(dl.FileName);

                currentFileName = dl.FileName;

                currentXml = root;

                refreshIdList();

                refreshPreview();
            }
        }

        private void refreshIdList()
        {
            var templates = currentXml.Elements();
            lstFeeds.Items.Clear();
            foreach (var template in templates)
            {
                lstFeeds.Items.Add(template.Element("id").Value);
            }
        }

        private void lstFeeds_SelectedIndexChanged(object sender, EventArgs e)
        {
            isEditModel = false;

            if (lstFeeds.SelectedItem != null)
            {
                string selId = lstFeeds.SelectedItem as String;
                thisTemplate = currentXml.Elements("feedTemplate")
                    .Where(p => p.Element("id").Value == selId)
                    .SingleOrDefault();

                txtId.Text = thisTemplate.Element("id").Value;
                txtName.Text = thisTemplate.Element("name").Value;
                txtType.Text = thisTemplate.Element("type").Value;
                txtBody.Text = thisTemplate.Element("body").Value;
                txtTitle.Text = thisTemplate.Element("title").Value;

                refreshFeedData();
            }
            isEditModel = true;
            refreshPreview();
        }

        private void refreshFeedData()
        {
            string content = txtTitle.Text + txtBody.Text;
            Regex regex = new Regex(
                @"(?<={)\w+(?=})",
                RegexOptions.IgnoreCase | RegexOptions.Compiled
                );
                        
            MatchCollection matches = regex.Matches(content);
            parameters = new Dictionary<string, string>();
            foreach (var match in matches)
            {
                if (!parameters.ContainsKey(match.ToString()))
                {
                    parameters.Add(match.ToString(), "888888");
                }
            }

            refreshFeedDataListBox();
        }

        class KeyValueItem
        {
            public string DisplayText { get; set; }
            public string Value { get; set; }
        }

        private void refreshFeedDataListBox()
        {
            lstFeedData.DataSource = null;

            var dataList = new List<KeyValueItem>();

            foreach (var keyValuePair in parameters)
            {
                dataList.Add(
                    new KeyValueItem()
                    {
                        DisplayText = string.Format("{0} : {1}", keyValuePair.Key, keyValuePair.Value),
                        Value = keyValuePair.Key
                    }
                    );
            }
            lstFeedData.DisplayMember = "DisplayText";
            lstFeedData.ValueMember = "Value";
            lstFeedData.DataSource = dataList;
            
        }
        
        private void txt_TextChanged(object sender, EventArgs e)
        {
            if (isEditModel)
            {
                refreshFeedData();
                refreshPreview();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            thisTemplate.Element("id").Value = txtId.Text;
            thisTemplate.Element("name").Value = txtName.Text;
            thisTemplate.Element("type").Value = txtType.Text;
            thisTemplate.Element("body").Value = txtBody.Text;
            thisTemplate.Element("title").Value = txtTitle.Text;
            MessageBox.Show("ok");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            currentXml.Save(currentFileName);
        }
        
        private void lstFeedData_DoubleClick(object sender, EventArgs e)
        {
            if (lstFeedData.SelectedItem != null)
            {
                string str = Microsoft.VisualBasic.Interaction.InputBox("请输入Data数据！", "建立方案", "", -1, -1);
                parameters[lstFeedData.SelectedValue as string] = str;
            }
            refreshFeedDataListBox();
            refreshPreview();
        }

        private void refreshPreview()
        {
            if (pForm == null)
            {
                pForm = new Preview();
                pForm.Show();
            }
            if (thisTemplate != null)
            {
                pForm.SetFeed(thisTemplate, txtTitle.Text + txtBody.Text, parameters);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pForm.Visible = !pForm.Visible;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Guid newId = Guid.NewGuid();
            XElement emptyFeedTemplate = new XElement("feedTemplate");
            emptyFeedTemplate.Add(new XElement("id", newId.ToString()));
            emptyFeedTemplate.Add(new XElement("name"));
            emptyFeedTemplate.Add(new XElement("body"));
            emptyFeedTemplate.Add(new XElement("title"));
            emptyFeedTemplate.Add(new XElement("type", "0"));
            currentXml.Add(emptyFeedTemplate);

            refreshIdList();

            lstFeeds.SelectedItem = newId.ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (lstFeeds.SelectedItem != null)
            {
                thisTemplate.Remove();
                refreshIdList();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            txtTitle.Text = "<ez:root xmlns:ez=\"http://www.codeplex.com/ezdeskdev\" section=\"title\"><ez:div></ez:div></ez:root>";
        }
    }
}
